package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * Servlet implementation class CheckMRXml
 */
// @WebServlet(description = "取得每月MR內容的清單", urlPatterns = { "/CheckMRXml" })
public class CheckMRXml extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckMRXml() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Connection conn = null;
		PreparedStatement ps = null;
		Widgets wid = new Widgets();
		ArrayList<MRFile> mrFiles = new ArrayList<MRFile>();
		Session session = null;
	    ChannelSftp sftp = null;

		try {
			JSch jsch = new JSch();
		    
		    
			conn = wid.getConn(true);
			String host = "10.80.71.9";
	    	String userId = "emome";
	    	String pwd = "icss880";
	    	JSch.setConfig("StrictHostKeyChecking", "no");
	    	session = jsch.getSession(userId, host);
			session.setPassword(pwd);
	        session.connect();
	        sftp = (ChannelSftp)  session.openChannel("sftp");
	        sftp.connect();
	        if ( sftp.isConnected() ) {
				Vector<?> fl = sftp.ls("../../icss/web/or/mr/xml");
	            for(int i=fl.size()-1; i>-1;i--){
	            	if ( fl.get(i).toString().contains("icssor 1") && !fl.get(i).toString().contains(".bak") ) {
	            		MRFile mrFile = new MRFile();
		            	mrFile.setFileName(fl.get(i).toString().substring(56, fl.get(i).toString().length()));
						mrFile.setYear(mrFile.getFileName().substring(7, 10));
						mrFile.setMonth(mrFile.getFileName().substring(10, 12));
						mrFiles.add(mrFile);	
	            	}
			    }
	        }
		} catch (Exception e) {
			MRFile mrFile = new MRFile();
			mrFile.setFileName(e.toString());
			mrFiles.add(mrFile);
			wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if ( ps != null ) { ps.close(); }
				if ( conn != null ) { conn.close(); }
				if ( sftp.isConnected() ) { sftp.disconnect(); }
				if ( session.isConnected() ) { session.disconnect(); }
			} catch (SQLException e) { }
		}
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(new Gson().toJson(mrFiles));
	}

	class MRFile {
		String year = "";
		String month = "";
		String fileName = "";

		public String getYear() {
			return this.year;
		}

		public void setYear(String paramString) {
			this.year = paramString;
		}

		public String getMonth() {
			return this.month;
		}

		public void setMonth(String paramString) {
			this.month = paramString;
		}

		public String getFileName() {
			return this.fileName;
		}

		public void setFileName(String paramString) {
			this.fileName = paramString;
		}
	}
}